cc.Class({
  extends: cc.Component,
  properties: {
    boxAdBtnPanel: cc.Node,
    buyBtn: cc.Node,
    freeBtn: cc.Node,
    nameLabel: cc.Label,
    priceLabel: cc.Label,
    boxSprite: cc.Sprite,
    baseItemNode: cc.Node,
    rewardParent: cc.Node
  },
  initBy: function (e, t, i, o) {
    undefined === i && (i = false);
    undefined === o && (o = null);
    this.boxId = e;
    this.shopBoxJson = t;
    this.isCanAdBuy = i;
    this.callBack = o;
    this.price = this.shopBoxJson["consume" + this.boxId];
    this.priceLabel.string = this.price;
    this.nameLabel.string = 1 == this.boxId ? "银色宝箱" : "黄金宝箱";
    var a = 1 == this.boxId ? "12" : "11";
    cc.pvz.utils.setSpriteFrame(this.boxSprite, "uiImage", "shop/shop" + a);
    this.onCheckBoxAdBtnGray(!this.isCanAdBuy);
    this.onBoxRewardShow();
    this.isCanAdBuy && 2 == this.boxId && cc.pvz.TAUtils.trackAdUIShow(cc.pvz.GameConfig.AdType.商店宝箱);
  },
  onBoxRewardShow: function () {
    var e = this.shopBoxJson["box" + this.boxId];
    var t = Math.floor(e.length / 2);
    var i = 0;
    this.rewardData = [];
    for (var o = 0; o < t; o++) {
      var a = cc.pvz.utils.getRewardItem(e[i], e[i + 1]);
      this.rewardData.push(a);
      i += 2;
    }
    this.baseItemNode.getComponent("RewardItem").initBoxRewardItem(this.rewardData[0]);
    for (var c = 1; c < t; c++) {
      var s = cc.instantiate(this.baseItemNode);
      s.parent = this.rewardParent;
      s.getComponent("RewardItem").initBoxRewardItem(this.rewardData[c]);
    }
  },
  onCheckBoxAdBtnGray: function (e) {
    if (1 == this.boxId) {
      this.boxAdBtnPanel.active = false;
      this.freeBtn.active = !e;
      this.buyBtn.active = e;
    } else {
      for (var t = 0; t < this.boxAdBtnPanel.childrenCount; t++) {
        this.boxAdBtnPanel.children[t].getComponent(cc.Sprite).setMaterial(0, cc.JsonControl.materialList[e ? 1 : 0]);
      }
      this.freeBtn.active = false;
    }
  },
  onClickAdBox: function () {
    var e = this;
    cc.UIShop.isLookAd || this.isCanAdBuy && (1 == this.boxId ? this.onAdClose(true) : (cc.UIShop.isLookAd = true, cc.pvz.AdUtils.showAdRewardVideo(cc.pvz.GameConfig.AdType.商店宝箱, function (t) {
      e.onAdClose(t);
    })));
  },
  onAdClose: function (e) {
    cc.UIShop.isLookAd = false;
    if (e) {
      cc.pvz.PlayerData.updateShopBoxDate(this.boxId, Date.now());
      this.isCanAdBuy && 2 == this.boxId && cc.pvz.PlayerData.updateShopBoxExp(this.price);
      cc.popupManager.removePopup(this);
      cc.popupManager.popup("mainUI", "ShopBoxEft", "UIShopBoxReward", {
        ad: false,
        scale: true
      }, this.boxId, this.rewardData, this.callBack);
    }
  },
  onClickBuyBox: function () {
    if (!cc.UIShop.isLookAd) {
      if (cc.pvz.PlayerData.isItemEnough(cc.pvz.GameConfig.ItemType.钻石, this.price)) {
        cc.pvz.PlayerData.updateShopBoxExp(this.price);
        cc.pvz.PlayerData.changeItemNum(cc.pvz.GameConfig.ItemType.钻石, -this.price);
        cc.popupManager.removePopup(this);
        cc.popupManager.popup("mainUI", "ShopBoxEft", "UIShopBoxReward", {
          ad: false,
          scale: true
        }, this.boxId, this.rewardData, function () {
          cc.UIShop.onChekBoxLvUp();
        });
      } else if (cc.pvz.PlayerData.isHaveAdBuyBankTimes(1)) {
        var e = cc.pvz.utils.getRewardItem(cc.pvz.GameConfig.ItemType.钻石, 100);
        cc.popupManager.popup("mainUI", "adbuyUI", "UIAdBuy", {
          ad: false,
          scale: true
        }, e);
      } else {
        cc.popupManager.showToast("钻石不足！");
      }
    }
  },
  onCloseUI: function () {
    cc.popupManager.removePopup(this);
  }
});